package cn.qingyun.gis.modules.baogan.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.qingyun.gis.modules.baogan.dto.PolDto;
import cn.qingyun.gis.modules.baogan.entity.MicroStreetData;
import cn.qingyun.gis.modules.baogan.enums.CareTypeEnum;
import cn.qingyun.gis.modules.baogan.service.MicroStreetDataService;
import cn.qingyun.gis.modules.baogan.vo.request.*;
import cn.qingyun.gis.modules.baogan.vo.response.ScenePersonAndTeamVo;
import cn.qingyun.gis.modules.init.system.vo.LoginUser;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.qingyun.gis.modules.baogan.mapper.MicrogridCareRecordMapper;
import cn.qingyun.gis.modules.baogan.entity.MicrogridCareRecordEntity;
import cn.qingyun.gis.modules.baogan.service.MicrogridCareRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
@DS("scenes_offensive")
public class MicrogridCareRecordServiceImpl extends ServiceImpl<MicrogridCareRecordMapper, MicrogridCareRecordEntity> implements MicrogridCareRecordService{

    @Autowired
    private MicroStreetDataService microStreetDataService;

    /**
     * 微网格-看管划分-新增或修改
     *
     * @param request
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateMicroGrid(MicroGridCareRecordSaveOrUpdateBatchRequest request) {
        //获取当前登录人信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //先删除全部
        List<Integer> sceneTypes = request.getList().stream()
                .map(MicroGridCareRecordAddOrUpdateRequest::getSceneType).collect(Collectors.toList());
        LambdaQueryWrapper<MicrogridCareRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(MicrogridCareRecordEntity::getMicroGridCode, request.getList().get(0).getMicroGridCode())
                .eq(MicrogridCareRecordEntity::getCareType, CareTypeEnum.SELF_PERSON_AND_TOTAL_TEAM.getCode())
                .in(MicrogridCareRecordEntity::getSceneType, sceneTypes);
        this.remove(queryWrapper);

        //新增
        List<MicrogridCareRecordEntity> addList = request.getList().stream().map(item -> {
            MicrogridCareRecordEntity microgridCareRecordEntity = new MicrogridCareRecordEntity();
            BeanUtils.copyProperties(item, microgridCareRecordEntity);
            microgridCareRecordEntity.setCreateBy(sysUser.getId());
            microgridCareRecordEntity.setCreateTime(DateUtil.date());
            microgridCareRecordEntity.setUpdateBy(sysUser.getId());
            microgridCareRecordEntity.setUpdateTime(DateUtil.date());
            return microgridCareRecordEntity;
        }).collect(Collectors.toList());
        this.saveBatch(addList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateStreet(StreetCareRecordSaveOrUpdateRequest request) {
        //获取当前登录人信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //先删除全部的
        LambdaQueryWrapper<MicrogridCareRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(MicrogridCareRecordEntity::getPoiId, request.getPoiId());
        this.remove(queryWrapper);
        //再新增
        if(CareTypeEnum.TEAM_DIRECT_SALE_PERSON.getCode().equals(request.getCareType())){
            //分配给团队人员
            Assert.isTrue(CollectionUtils.isNotEmpty(request.getTeamNumberRequestList()),"团队人员不能为空");
            request.getTeamNumberRequestList().forEach(item->{
                MicrogridCareRecordEntity microgridCareRecordEntity = new MicrogridCareRecordEntity();
                BeanUtils.copyProperties(request, microgridCareRecordEntity);
                microgridCareRecordEntity.setCaregiver(item.getCaregiver());
                microgridCareRecordEntity.setStaffWorkNo(item.getStaffWorkNo());
                microgridCareRecordEntity.setCreateBy(sysUser.getId());
                microgridCareRecordEntity.setCreateTime(DateUtil.date());
                microgridCareRecordEntity.setUpdateBy(sysUser.getId());
                microgridCareRecordEntity.setUpdateTime(DateUtil.date());
                this.save(microgridCareRecordEntity);
            });
        }else {
            //分配给渠道
            Assert.isTrue(StringUtil.isNotEmpty(request.getChannelCode()) || StringUtil.isNotEmpty(request.getChannelName()),
                    "渠道编码、渠道名称-不能为空");
            MicrogridCareRecordEntity microgridCareRecordEntity = new MicrogridCareRecordEntity();
            BeanUtils.copyProperties(request, microgridCareRecordEntity);
            microgridCareRecordEntity.setCreateBy(sysUser.getId());
            microgridCareRecordEntity.setCreateTime(DateUtil.date());
            microgridCareRecordEntity.setUpdateBy(sysUser.getId());
            microgridCareRecordEntity.setUpdateTime(DateUtil.date());
            this.save(microgridCareRecordEntity);
        }
        //更新沿街名称
        LambdaUpdateWrapper<MicroStreetData> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(MicroStreetData::getId,request.getPoiId())
                .set(MicroStreetData::getName,request.getName())
                .set(MicroStreetData::getUpdateBy,sysUser.getId())
                .set(MicroStreetData::getUpdateTime,DateUtil.date());
        microStreetDataService.update(updateWrapper);
    }

    /**
     * 查询场景及所选自有人员、团队列表
     *
     * @param request
     * @return
     */
    @Override
    public List<ScenePersonAndTeamVo> listScenePersonAndTeamVo(MicroGridCodeRequest request) {
        return this.baseMapper.listScenePersonAndTeamVo(request);
    }

    @Override
    public PolDto selectWKTLogData(String microGridCode, String id) {
        return this.baseMapper.selectWKTLogData(microGridCode,id);
    }

    /**
     * 查询新增的面中是否存在已有人员包干的点
     *
     * @param request
     * @return
     */
    @Override
    public boolean hasCarePointInAddWkt(HasCarePointInAddWktRequest request) {
        Integer count = this.baseMapper.hasCarePointInAddWkt(request);
        return count>0?true:false;
    }

    /**
     * 删除看管面中的看管点信息
     *
     * @param wkt
     * @param sceneTypeList
     */
    @Override
    public void deleteCareInfoInWkt(String wkt,List<Integer> sceneTypeList) {
        this.baseMapper.deleteCareInfoInWkt(wkt,sceneTypeList);
    }

}
